<?php
/**
 * View  LaFabbrica.org
 * 
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @created    17-mag-2011
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 */
?>
<div id="container" class="fancybox">
    <script type="text/javascript">
	//<![CDATA[
	
	$(document).ready(function() {

	    var validator = $("#contactForm").validate({
		invalidHandler: function(e, validator) {
		    var errors = validator.numberOfInvalids();
		    if (errors) {
			$("ul#errorBox").show();
		    } else {
			$("ul#errorBox").hide();
		    };
		},
	    
		// Assegna classe 'error' al <li> dell'elemento invalido
		errorPlacement: function(error, element) {
		    //console.log(element);
		    element.parent("span").parent("div").parent("li").addClass("error");
		    error.appendTo( element.parent("span").parent("div"));
		},
		submitHandler: function(form) {
		    //$('#salva').attr('disabled', 'disabled');
		    $.fancybox.showActivity();

		    $.ajax({
			    type	: "POST",
			    cache	: false,
			    url		: $(form).attr('action'),
			    data	: $(form).serializeArray(),
			    success: function(data) {
				    $.fancybox(data,{
                                        'showCloseButton' : false,
                                        'onClosed' : function(){
                                            $('#contacts').pagination({
                                                        'targetUrl' : '/admin/ajax/list/contactsmini',
                                                        'orderField' : 'contact_name',
                                                        'orderDir' : 'asc'
                                                    }, selectable
					    );
                                        }                                        
                                    });
			    }
		    });

		    return false;
		},	    
		errorElement: "p",
		groups: {
		    contact_name : "contact_firstname contact_lastname",
		    contact_phone: "contact_phone_prefix contact_phone_number"
		},
		rules: {
		    contact_firstname:{
			required: true
		    },
		    contact_lastname: {
			required: true
		    },
		    contact_email: {
			required: true,
			email: true
		    },
		    contact_phone_prefix: {
			required: true,
			digits: true,
			rangelength: [1, 4]
		    },
		    contact_phone_number: {
			required: true,
			digits: true,
			rangelength: [1, 10]
		    }		
		},
		messages: {
		    contact_firstname: {
			required: "<?php echo ERR_EMPTY ?>"
		    },
		    contact_lastname: {
			required: "<?php echo ERR_EMPTY ?>"
		    },
		    contact_email : {
			required: "<?php echo ERR_EMPTY ?>",
			email: "<?php echo ERR_EMAIL ?>"
		    },
		    contact_phone_prefix: {
			required: "<?php echo ERR_EMPTY ?>",
			digits : "<?php echo sprintf( ERR_FORMAT, 'Tel. Principale' ) ?>",
			rangelength : "<?php echo sprintf( ERR_FORMAT, 'Tel. Principale' ) ?>"
		    },
		    contact_phone_number: {
			required: "<?php echo ERR_EMPTY ?>",
			digits : "<?php echo sprintf( ERR_FORMAT, 'Tel. Principale' ) ?>",
			rangelength : "<?php echo sprintf( ERR_FORMAT, 'Tel. Principale' ) ?>"
		    }

		},
		debug:true
	    });
	});
    
	$("#salva").click(function() {
	    validator.form();
	});
	
	$("#annulla").click(function(){
	    $.fancybox.close();
	});

	//]]>
    </script>

    <div class="info">
	<h2>Inserisci Nuovo Contatto</h2>
	<div>Il contatto verrà aggiunto automaticamente alla lista della pagina precedente</div>
	<div>I campi contrassegnati da <span class="req">*</span> sono obbligatori!</div>
    </div>

    <form class="fab fancybox" name="contactForm" id="contactForm" action="/admin/ajax/form/newcontact" method="post" enctype="multipart/form-data">
	<ul id="errorBox">
	    <li id="errorLi" style="<?php echo $this->errorMsg ? '' : 'display:none' ?>">
		<h3 id="errorMsgLbl">Si &egrave; verificato un problema durante l'invio del modulo.</h3>
		<p id="errorMsg">
			Di seguito trovi evidenziati gli errori riscontrati.
		</p>
	    </li>
	    <li class="<?php echo $this->errorMsg['contact_name'] ? 'error' : '' ?>">
		<div class="float-left">
		    <label class="desc <?php echo $this->errorMsg['contact_name'] ? 'error' : '' ?>">Nome<span class="req">*</span></label>
		    <span class="input">
			<input id="contact_firstname" name="contact_firstname" class="field text" size="20" type="text" maxlength="255" value="<?php echo $this->data['contact_firstname'] ?>" />
			<label>Nome</label>
		    </span>

		    <span class="input">
			<input id="contact_lastname" name="contact_lastname" class="field text" size="20" type="text" maxlength="255" value="<?php echo $this->data['contact_lastname'] ?>" />
			<label>Cognome</label>
		    </span>
		</div>
		<p class="error" style="<?php echo $this->errorMsg['contact_name'] ? 'display:block' : '' ?>"><?php echo $this->errorMsg['contact_name'] ?></p>
	    </li>

	    <li class="<?php echo $this->errorMsg['contact_email'] ? 'error' : '' ?>">
		<label class="desc <?php echo $this->errorMsg['contact_email'] ? 'error' : '' ?>">Email Principale<span class="req">*</span></label>
		<div>
		    <span>
			<input id="contact_email" name="contact_email" class="field text medium" type="text" maxlength="255" value="<?php echo $this->data['contact_email'] ?>" /> 
		    </span>
		</div>
		<p class="error" style="<?php echo $this->errorMsg['contact_email'] ? 'display:block' : '' ?>"><?php echo $this->errorMsg['contact_email'] ?></p>
	    </li>

	    <li class="<?php echo $this->errorMsg['contact_phone'] ? 'error' : '' ?>">
		<label class="desc <?php echo $this->errorMsg['contact_phone'] ? 'error' : '' ?>">Tel. Principale<span class="req">*</span></label>
		<div class="float-left">
		    <span class="input">
			<input class="field text" id="contact_phone_prefix" name="contact_phone_prefix" type="text" size="4" maxlength="4" value="<?php echo $this->data['contact_phone_prefix'] ?>" /> / 
			<label>Pref</label>
		    </span>

		    <span class="input">
			<input class="field text" id="contact_phone_number" name="contact_phone_number" type="text" size="10" value="<?php echo $this->data['contact_phone_number'] ?>" /> 
			<label>Numero</label>
		    </span>
		</div>
		<p class="error" style="<?php echo $this->errorMsg['contact_phone'] ? 'display:block' : '' ?>"><?php echo $this->errorMsg['contact_phone'] ?></p>
	    </li>

	</ul>
	<div id="formButtons">
	    <button id="salva" type="submit" name="salva" value="salva" class="button positive">
		<img src="/images/admin/tick.png" alt="" />Salva
	    </button>
	    <button id="annulla" type="reset" name="annulla" value="annulla" class="button negative">
		<img src="/images/admin/cross.png" alt="" />Annulla
	    </button>	    
	</div>
    </form>
</div>

<img id="bottom" class="fancybox" src="/images/admin/bottom.png" alt="" />
